package com.chenfan.ccp.plug.rpc.service.impl;

import com.chenfan.infra.constants.CommonBeanConstants;
import com.chenfan.ccp.plug.rpc.service.NotifyRemoteService;
import com.chenfan.ccp.util.start.ApplicationContextUtil;
import com.chenfan.ccp.util.tool.HttpUtil;
import com.chenfan.ccp.util.tool.JsonUtil;
import com.chenfan.ccp.plug.mq.util.MqTemplate;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class DefaultNotifyRemoteServiceImpl implements NotifyRemoteService {

    private final String LINK = ":";
    private String messageTopic;
    private String getCountUrl;
    private String getAllUrl;
    private String finishUrl;

    private final LoadingCache<String, String> cache = Caffeine.newBuilder()
            .maximumSize(10_000)
            .expireAfterWrite(5, TimeUnit.MINUTES)
            .refreshAfterWrite(1, TimeUnit.MINUTES)
            .build(param -> HttpUtil.get(getAllUrl, param));


    @Value("${pass.notify.url}")
    public void setUrl(String url) {
        this.getAllUrl = url + "getAll";
        this.getCountUrl = url + "getCount";
        this.finishUrl = url +"/notify/approval/";
    }

    @Value("${pass.notify.topic}")
    public void setTopic(String topic) {
        this.messageTopic = topic;
    }


    @Override
    public void modifyNotify(Long id, Integer notifyProcess, Integer notifyType,
                             Integer messageType, Integer messageProgress) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectName", ApplicationContextUtil.getServiceName());
        param.put("notifyProcess", notifyProcess == null ? "" : notifyProcess);
        param.put("notifyType", notifyType == null ? "" : notifyType);
        param.put("messageType", messageType == null ? "" : messageType);
        param.put("messageProgress", messageProgress == null ? "" : messageProgress);
        MqTemplate.sendMsg(messageTopic, JsonUtil.obj2String(param));
    }

    @Override
    public void saveNotify(Long businessId,Integer notifyType, Integer notifyProcess,
                    Long messageType,String messageTypeName,String messageUrl,String jumpUrlType,
                    String message, Long userId, String userName,Long approvalId,String approvalCode,
                    String businessCode, Integer messageProgress){
        Map<String, Object> param = new HashMap<>();
        param.put("businessId", businessId);
        param.put(CommonBeanConstants.USER_ID, userId);
        param.put("userName", userName);
        param.put("projectName", ApplicationContextUtil.getServiceName());
        param.put("message", message);
        param.put("notifyType", notifyType == null ? "" : notifyType);
        param.put("notifyProcess", notifyProcess == null ? "" : notifyProcess);
        param.put("messageType", messageType == null ? "" : messageType);
        param.put("messageTypeValue", messageTypeName == null ? "" : messageTypeName);
        param.put("messageProgress", messageProgress == null ? "" : messageProgress);
        param.put("messageUrl", messageUrl == null ? "" : messageUrl);
        param.put("jumpUrlType", jumpUrlType == null ? "" : jumpUrlType);
        //审批实例id
        param.put("approvalId", approvalId);
        //审批模板id
        param.put("approvalCode", approvalCode);
        param.put("businessCode", businessCode);
        MqTemplate.sendMsg(messageTopic, JsonUtil.obj2String(param));
    }

    @Override
    public Integer getCount(Integer notifyProcess, Integer notifyType, Integer messageType, Integer messageProgress) {

        Map<String, Object> param = new HashMap<>();
        param.put("projectName", ApplicationContextUtil.getServiceName());
        param.put("notifyProcess", notifyProcess == null ? "" : notifyProcess);
        param.put("notifyType", notifyType == null ? "" : notifyType);
        param.put("messageType", messageType == null ? "" : messageType);
        param.put("messageProgress", messageProgress == null ? "" : messageProgress);
        String result = HttpUtil.get(getCountUrl, param);

        return (result == null || result.isEmpty()) ? 0 : Integer.valueOf(result);

    }

    @Override
    public <T> List<T> getAll(Integer notifyProcess, Integer notifyType,
                              Integer messageType, Integer messageProgress, Integer page, Integer size) {


        Map<String, Object> param = new HashMap<>();
        param.put("projectName", ApplicationContextUtil.getServiceName());
        param.put("notifyProcess", notifyProcess == null ? "" : notifyProcess);
        param.put("notifyType", notifyType == null ? "" : notifyType);
        param.put("messageType", messageType == null ? "" : messageType);
        param.put("messageProgress", messageProgress == null ? "" : messageProgress);
        param.put("page", page);
        param.put("size", size);

        return JsonUtil.string2Obj(cache.get(JsonUtil.obj2String(page)), List.class);
    }

    @Override
    public void approvalFinish(Long approvalId) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("projectName", ApplicationContextUtil.getServiceName());
        HttpUtil.get( finishUrl + approvalId, param);
    }
}
